package com.tinyseed.emcp.api.maintenance.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.tinyseed.emcp.api.basicdata.model.OrganizeOutputDTO;
import com.tinyseed.emcp.api.basicdata.service.OrganizeService;
import com.tinyseed.emcp.api.maintenance.domain.MaintenanceInfo;
import com.tinyseed.emcp.api.maintenance.domain.MaintenanceInfoDTO;
import com.tinyseed.emcp.api.maintenance.domain.MaintenanceInfoMapper;
import com.tinyseed.emcp.api.maintenance.domain.TelReturnRecord;
import com.tinyseed.emcp.api.maintenance.domain.query.MaintenanceInfoQueryCriteria;
import com.tinyseed.emcp.api.maintenance.repository.MaintenanceRepository;
import com.tinyseed.emcp.api.maintenance.repository.TelReturnRecordRepository;
import com.tinyseed.emcp.api.maintenance.state.*;
import com.tinyseed.hmxx.common.utils.PageUtil;
import com.tinyseed.hmxx.common.utils.QueryHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @version Revision 1.0.0
 * @版权：
 * @author: Howard
 * @email:
 * @see:
 * @创建日期：2020/3/15
 * @功能说明：维保实现类
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2020/3/15 wuxiran        创建
 * @end
 */
@Service
public class MaintenanceServiceImpl implements MaintenanceService {

    @Autowired
    private MaintenanceRepository maintenanceRepository;

    @Autowired
    private MTNewState mtNewState;

    @Autowired
    private MTAuditedState mtAuditedState;

    @Autowired
    private MTIssuedState mtIssuedState;

    @Autowired
    private MTInExecutionState mtInExecutionState;

    @Autowired
    private MTSubmittedState mtSubmittedState;

    @Autowired
    private MaintenanceInfoMapper maintenanceInfoMapper;

    @Autowired
    private OrganizeService organizeService;

    @Autowired
    private TelReturnRecordRepository telReturnRecordRepository;

    @Override
    public void save(MaintenanceInfo maintenanceInfo) {
        maintenanceRepository.save(maintenanceInfo);
    }


    private MaintenanceInfo buildMaintenanceInfo(MaintenanceInfo target){
        Optional<MaintenanceInfo> source = maintenanceRepository.findById(target.getId());
        source.ifPresent(info->{
            BeanUtil.copyProperties(info,target, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        });



        return target;
    }

    @Override
    public void auditedMT(MaintenanceInfo maintenanceInfo) {

        MaintenanceInfo temp = buildMaintenanceInfo(maintenanceInfo);
        mtNewState.nextStep(temp);
    }
    @Override
    public void issuedMT(MaintenanceInfo maintenanceInfo) {
        /**
         * Description distributed下发维保单到维保人员
         * Version 1.0 
         * Date 2020/3/15 10:09 下午 
         * Author howard
         * @param maintenanceInfo
         * @return void
         */
        MaintenanceInfo temp = buildMaintenanceInfo(maintenanceInfo);

        mtAuditedState.nextStep(temp);
    }
    @Override
    public void confirmMT(MaintenanceInfo maintenanceInfo) {
        MaintenanceInfo temp = buildMaintenanceInfo(maintenanceInfo);

        mtIssuedState.nextStep(temp);
    }
    @Override
    public void submit(MaintenanceInfo maintenanceInfo) {
        MaintenanceInfo temp = buildMaintenanceInfo(maintenanceInfo);

        mtInExecutionState.nextStep(temp);
    }
    @Override
    public void complete(MaintenanceInfo maintenanceInfo) {
        MaintenanceInfo temp = buildMaintenanceInfo(maintenanceInfo);

        mtSubmittedState.nextStep(temp);
    }

    @Override
    public void addTelReturnRecord(MaintenanceInfoDTO telReturnRecord) {

        Date date = new Date();
        Timestamp nousedate = new Timestamp(date.getTime());

        TelReturnRecord item = TelReturnRecord.builder()
                .callResult(telReturnRecord.getCallResult())
                .reasonForCall(telReturnRecord.getReasonForCall())
                .enterprise(telReturnRecord.getEnterprise())
                .callTime(nousedate)
                .shouldMaintenance(telReturnRecord.getShouldMaintenance())
                .build();

        telReturnRecordRepository.save(item);
    }

    @Override
    public Page<TelReturnRecord> getTelReturnRecord(Pageable pageable) {
        return telReturnRecordRepository.findAll(pageable);
    }

    @Override
    public Object queryAll(MaintenanceInfoQueryCriteria criteria, Pageable pageable) {

        Optional.ofNullable(criteria.getOrganizeId()).ifPresent(organizeId ->{
            List<OrganizeOutputDTO> organizeOutputDTOS = organizeService.getOrganizeIdListByid(Long.valueOf(organizeId));
            criteria.setOrganizeName(organizeOutputDTOS.stream().map(OrganizeOutputDTO::getOrganizeName).collect(Collectors.toList()) );
        });
        Page<MaintenanceInfo> page = maintenanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(maintenanceInfoMapper::toDto));
    }

}
